import java.util.concurrent.CountDownLatch;

/**
 * CountDownLatch 允许多个或一个线程等待其他多个或者一个线程的倒计数
 * 来继续执行线程的后面的任务
 */
public class CountDownLatchTest {

	public static void main(String[] args) {
		final CountDownLatch player = new CountDownLatch(1);
		final CountDownLatch judge = new CountDownLatch(3);
		for(int i=0;i<3;i++){
			new Thread(new Runnable(){
				public void run() {
					System.out.println(Thread.currentThread().getName()+"准备就位.");
					try {
						player.await();//等待通知
						System.out.println(Thread.currentThread().getName()+"枪响!冲刺...");
						Thread.sleep((long)Math.random()*10000);
					} catch (Exception e) {
						e.printStackTrace();
					}
					System.out.println(Thread.currentThread().getName()+"到达!");
					judge.countDown();//到达通知
				}
			}).start();
		}
		
		try {
			Thread.currentThread().sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		player.countDown();
		System.out.println("发令!");
		try {
			judge.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("结束!");
	}

}
